<?php
//把textarea内容转换为<p>标签
function changetop($str)
{
	$arr = explode("\n",$str);
	if($arr)
	{
		foreach($arr AS $key=>$value)
		{
			$result .= "<p>".$value."</p>";
		}
		return $result;
	}
}

//获取系统组自定义图片尺寸
function picsize($id)
{
	global $DB,$prefix,$system;
	$picsize = $DB->qgGetOne("SELECT width,height FROM ".$prefix."sysgroup WHERE id='".$id."'");
	if($picsize["width"]==0 || $picsize["height"]==0)
	{
		$picsize["width"] = $system["markwidth"];
		$picsize["height"] = $system["markheight"];
	}
	return $picsize;
}

//获取图片数组
function get_thumbarr($id)
{
	global $DB,$prefix;
	$id = intval($id);
	if($id!="0")
	{
		$sql = "SELECT folder,filename,markfile,thumbfile FROM ".$prefix."upfiles WHERE id='".$id."'";
		$rs = $DB->qgGetOne($sql);
		return $rs;
	}
}

//站点路径
function site_uri()
{
	$iparr = explode(".",$_SERVER['SERVER_ADDR']);
	if($iparr[0]=="127")
	{
		$siteurlnew = substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'/')+1);
	}
	else
	{
		if($iparr[0]=="192" && $iparr[1]=="168")
		{
			$siteurlnew = substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'/')+1);
		}
		else
		{
			$siteurlnew = "/";
		}
	}
	return $siteurlnew;
}

#[查找关联字段组id]
/*从msg开始查找，找不到往分类查找，再找不到往父分类查找，以此类推*/
function upfieldid($msgid)
{
	global $DB,$prefix;
	$rs = $DB->qgGetOne("SELECT * FROM ".$prefix."msg WHERE id='".$msgid."'");
	if($rs["fieldgroupid"])
	{
		$fieldgroupid = $rs["fieldgroupid"];
	}
	else
	{
		//查找主题上级信息
		if($rs["additonid"]!=0)
		{
			return upfieldid($rs["additonid"]);
		}
		else
		{
			$fieldgroupid = field_cateid($rs["cateid"]);
		}
	}
	return $fieldgroupid;
}

//循环读取上级分类，直到存在或到达根分类为止
function field_cateid($cateid)
{
	global $DB,$prefix;
	$rs = $DB->qgGetOne("SELECT parentid,fieldgroupid FROM ".$prefix."category WHERE id='".$cateid."'");
	if($rs["fieldgroupid"])
	{
		return $rs["fieldgroupid"];
	}
	else
	{
		if($rs["parentid"]==0)
		{
			return 0;
		}
		else
		{
			return field_cateid($rs["parentid"]);
		}
	}
}

#[合并重复语言版本]
function lanmerge($array,$name)
{
	if(!$array[$name] && $array[$name."_en"])
	{
		$array[$name] = $array[$name."_en"];
	}
	if(!$array[$name] && $array[$name."_ft"])
	{
		$array[$name] = $array[$name."_ft"];
	}
	if($array[$name] == $array[$name."_en"])
	{
		unset($array[$name."_en"]);
	}
	if($array[$name] == $array[$name."_ft"])
	{
		unset($array[$name."_ft"]);
	}
	return $array;
}

//获得域名
function getServerName() 
{ 
	$ServerName = strtolower($_SERVER['SERVER_NAME']?$_SERVER['SERVER_NAME']:$_SERVER['HTTP_HOST']); 
	if(strpos($ServerName,'http://'))
	{
		return str_replace('http://','',$ServerName); 
	}
	return $ServerName; 
}

//表单提交数据关联后台字段名称
function form_field($form_sign,$msg_field)
{
	global $DB,$prefix;
	$form = $DB->qgGetOne("SELECT * FROM ".$prefix."form_cate WHERE sign='".$form_sign."'");
	$i = 0;
	foreach($msg_field AS $key=>$value)
	{
		$rsc = $DB->qgGetOne("SELECT field_name FROM ".$prefix."form_field WHERE formid='".$form["id"]."' AND sign='".$key."' AND ifcheck=1");
		if($rsc)
		{
			$msg_fieldlist[$i]["sign"] = $key;
			$msg_fieldlist[$i]["field_name"] = $rsc["field_name"];
			$msg_fieldlist[$i]["field_content"] = $value;
			$i++;
		}
	}
	return $msg_fieldlist;
}

//询盘表转换成产品列表
function cart_msg($rslist)
{
	global $DB,$prefix;
	$i = 1;
	foreach($rslist AS $key=>$value)
	{
		$rsc = $DB->qgGetOne("SELECT m.*,u.folder,u.filename,u.thumbfile,u.markfile FROM ".$prefix."msg AS m LEFT JOIN ".$prefix."upfiles AS u ON m.thumb=u.id WHERE m.id='".$value["msgid"]."'");
		$value["id"] = $rsc["id"];
		$value["subject"] = $rsc["subject".$bi];
		$value["thumb"] = $rsc["thumb"];
		$value["folder"] = $rsc["folder"];
		$value["filename"] = $rsc["filename"];
		$value["thumbfile"] = $rsc["thumbfile"];
		$value["markfile"] = $rsc["markfile"];
		$value["row_index"] = $i;
		$i++;
		$msglist[] = $value;
	}
	return $msglist;
}
?>